package com.css.dzj.dzjdzqs.modules.apaction.appunish.service.impl;

import com.alibaba.fastjson.JSON;
import com.css.dzj.dzjdzqs.common.util.HttpClientUtil;
import com.css.dzj.dzjdzqs.modules.apaction.appunish.Vo.ApPunishVo;
import com.css.dzj.dzjdzqs.modules.apaction.appunish.entity.ApPunishEntity;
import com.css.dzj.dzjdzqs.modules.apaction.appunish.mapper.ApPunishMapper;
import com.css.dzj.dzjdzqs.modules.apaction.appunish.service.ApPunishService;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author WT
 * @Date 2020/6/28
 */
@Service
public class ApPunishServiceImpl implements ApPunishService {

    @Autowired
    private ApPunishMapper apPunishMapper;
    @Autowired
    JPAQueryFactory jpaQueryFactory;
    @Value("${AP_URL}")
    private String apUrl;

    @Override
    @Transactional
    public void add(List<ApPunishVo> apPunishVoList) {

        try {
            for(ApPunishVo apPunishVo :apPunishVoList){
                ApPunishEntity apPunishEntity = new ApPunishEntity();
                apPunishEntity.setId(apPunishVo.getId());
                apPunishEntity.setRecordUniqueIdentity(apPunishVo.getRecordUniqueIdentity());
                apPunishEntity.setSuperviseItemCode(apPunishVo.getSuperviseItemCode());
                apPunishEntity.setPunishActionName(apPunishVo.getPunishActionName());
                apPunishEntity.setPunishActionCode(apPunishVo.getPunishActionCode());
                apPunishEntity.setImplementInstitution(apPunishVo.getImplementInstitution());
                apPunishEntity.setImplementInstitutionCode(apPunishVo.getImplementInstitutionCode());
                apPunishEntity.setEntrustDept(apPunishVo.getEntrustDept());
                apPunishEntity.setEntrustDeptCode(apPunishVo.getEntrustDeptCode());
                apPunishEntity.setSuperviseObject(apPunishVo.getSuperviseObject());
                apPunishEntity.setCaseSource(apPunishVo.getCaseSource());
                apPunishEntity.setCheckActionCode(apPunishVo.getCheckActionCode());
                apPunishEntity.setAdministrativeCp(apPunishVo.getAdministrativeCp());
                apPunishEntity.setAdministrativeCpNa(apPunishVo.getAdministrativeCpNa());
                apPunishEntity.setAdministrativeCpCeType(apPunishVo.getAdministrativeCpCeType());
                apPunishEntity.setAdministrativeCpUiCode(apPunishVo.getAdministrativeCpUiCode());
                apPunishEntity.setAddressRegistered(apPunishVo.getAddressRegistered());
                apPunishEntity.setAddressOperating(apPunishVo.getAddressOperating());
                apPunishEntity.setAreaNumber(apPunishVo.getAreaNumber());
                apPunishEntity.setIllegalFact(apPunishVo.getIllegalFact());
                apPunishEntity.setPunishAccord(apPunishVo.getPunishAccord());
                apPunishEntity.setPunishProcedure(apPunishVo.getPunishProcedure());
                apPunishEntity.setIfHearingWitnesses(apPunishVo.getIfHearingWitnesses());
                apPunishEntity.setFilingDate(apPunishVo.getFilingDate());
                apPunishEntity.setCaseInvolvingPunishment(apPunishVo.getCaseInvolvingPunishment());
                apPunishEntity.setMajorCase(apPunishVo.getMajorCase());
                apPunishEntity.setLegalReview(apPunishVo.getLegalReview());
                apPunishEntity.setLegalReviewDate(apPunishVo.getLegalReviewDate());
                apPunishEntity.setLegalReviewOpinions(apPunishVo.getLegalReviewOpinions());
                apPunishEntity.setDiscussCollectively(apPunishVo.getDiscussCollectively());
                apPunishEntity.setGroupDiscussionDate(apPunishVo.getGroupDiscussionDate());
                apPunishEntity.setGroupDiscussionConclusion(apPunishVo.getGroupDiscussion());
                apPunishEntity.setPunishDocumentCode(apPunishVo.getPunishDocumentCode());
                apPunishEntity.setSetDate(apPunishVo.getSetDate());
                apPunishEntity.setPunishType(apPunishVo.getPunishType());
                if(null==apPunishVo.getFine()){
                    apPunishEntity.setFine(null);
                }else{
                    apPunishEntity.setFine(BigDecimal.valueOf(apPunishVo.getFine()));
                }
                apPunishEntity.setPenaltyResult(apPunishVo.getPenaltyResult());
                apPunishEntity.setAdministrativeReconsider(apPunishVo.getAdministrativeReconsider());
                apPunishEntity.setAdministrativeAction(apPunishVo.getAdministrativeAction());
                apPunishEntity.setCasesClosedExecutionWay(apPunishVo.getCasesClosedExecutionWay());
                apPunishEntity.setCasesClosedExecutionResult(apPunishVo.getCasesClosedExecutionResult());
                if(null==apPunishVo.getCasesClosedFine()){
                    apPunishEntity.setCasesClosedFine(null);
                }else{
                    apPunishEntity.setCasesClosedFine(BigDecimal.valueOf(apPunishVo.getCasesClosedFine()));
                }
                apPunishEntity.setCasesClosedExecution(apPunishVo.getCasesClosedExecution());
                apPunishEntity.setAdministrativePenalties(apPunishVo.getAdministrativePenalties());
                apPunishEntity.setOtherDisposals(apPunishVo.getOtherDisposals());
                apPunishEntity.setDeportationDate(apPunishVo.getDeportationDate());
                apPunishEntity.setSubmittedDate(apPunishVo.getSubmittedDate());
                apPunishEntity.setSubmittedPersonnel(apPunishVo.getSubmittedPersonnel());
                apPunishMapper.save(apPunishEntity);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    @Transactional
    public void pushtPunish() {

        try{
            String msg = HttpClientUtil.doGet(apUrl+"/punish/getApList");
            List<ApPunishVo> apPunishVoList = JSON.parseArray(msg, ApPunishVo.class);
            if(null!=apPunishVoList && apPunishVoList.size()>0){
                add(apPunishVoList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
